package com.hl.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.Publicity;
import com.hl.sfzy.service.PublicityService;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/publicity")
public class PublicityController {
    @Autowired
    private PublicityService publicityService;

    @GetMapping("/{id}")
    public R find(@PathVariable("id") Long id){
        Publicity publicity = publicityService.findById(id);
        return R.ok(publicity);
    }
    @GetMapping("/findAll")
    public R findAll(
            @RequestParam(required = false,defaultValue="1") int pageNum,
            @RequestParam(required = false,defaultValue = "5") int pageSize,
            Publicity publicity
    ){
        PageInfo<Publicity> pageInfo = publicityService.findByPage(pageNum, pageSize, publicity);
        return R.ok(pageInfo);
    }
    @PostMapping("/add")
    public R add(@RequestBody Publicity publicity){
        int i = publicityService.add(publicity);
        if (i>0){
            return R.ok("添加成功");
        }else {
            return R.error("添加失败");
        }
    }
    @PutMapping("/edit")
    public R edit(@RequestBody Publicity publicity){
        int i = publicityService.edit(publicity);
        if (i>0){
            return R.ok("修改成功");
        }else {
            return R.error("修改失败");
        }
    }
    @DeleteMapping("/{id}")
    public R remove(@PathVariable("id") Long id){
        int i = publicityService.removeByPrimaryKey(id);
        if (i>0){
            return R.ok("删除成功");
        }else {
            return R.error("删除失败");
        }
    }
    @DeleteMapping("/removeBatch")
    public R removeBatch(@RequestBody List<Long> ids){
        int i = publicityService.deleteBatch(ids);
        if (i>0){
            return R.ok("删除成功");
        }else {
            return R.error("删除失败");
        }
    }
}
